package com.lg.partition12;

import java.io.*;
import java.util.Arrays;

/**
 * @author `RKC`
 * @date 2022/4/20 10:01
 */
public class P1111修复公路 {

    private static final int N = 1010, M = 100010;
    private static Edge[] edges = new Edge[M];
    private static int[] parent = new int[N];
    private static int n, m;

    private static StreamTokenizer tokenizer = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    private static PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        n = nextInt();
        m = nextInt();
        for (int i = 1; i <= n; i++) parent[i] = i;
        for (int i = 0; i < m; i++) {
            int u = nextInt(), v = nextInt(), t = nextInt();
            edges[i] = new Edge(u, v, t);
        }
        Arrays.sort(edges, 0, m);
        int cnt = n, ans = 0;
        for (int i = 0; i < m; i++) {
            int u = edges[i].u, v = edges[i].v, t = edges[i].t;
            int fu = find(u), fv = find(v);
            if (fu != fv) {
                parent[fu] = fv;
                cnt--;
                ans = Math.max(ans, t);
            }
        }
        if (cnt > 1) ans = -1;
        writer.println(ans);
        writer.flush();
    }

    private static int find(int x) {
        if (x != parent[x]) parent[x] = find(parent[x]);
        return parent[x];
    }

    private static class Edge implements Comparable<Edge> {
        private int u, v, t;

        public Edge(int u, int v, int t) {
            this.u = u;
            this.v = v;
            this.t = t;
        }

        @Override
        public int compareTo(Edge o) {
            return Integer.compare(t, o.t);
        }
    }

    private static int nextInt() throws IOException {
        tokenizer.nextToken();
        return (int) tokenizer.nval;
    }
}
